<?php

declare(strict_types=1);

namespace App\Model\PssModel;

use App\Model\BaseModel\BasePssModel;
use Hyperf\DbConnection\Db;
use App\Service\BaseService;
use App\Service\Code\Normal;

class ShopRecommendBrand extends BasePssModel
{
    public $table = 'shop_recommend_brand';
    const REDIS_CACHE_EXPIRE    = 60;
    const REDIS_EMPTY_NULL_TAG  = 'null';
    const REDIS_RDB_NUM         = 10;
    /**
     * 获取推荐品牌列表
     */
    public function getRecommandBrandList()
    {
        $redis = BaseService::instance()->getRedisObj();
        $redis->select(self::REDIS_RDB_NUM);
        $key   = $this->table;
        $lists = $redis->get($key);
        if( !empty($lists) ) {
            if( $lists == self::REDIS_EMPTY_NULL_TAG ) {
                return Normal::returnCode(Normal::SUC,"暂无数据");
            }
            
            $lists = json_decode($lists,true);
            return Normal::returnCode(Normal::SUC,"获取数据成功",[
                'recommandBrandList' => $lists
            ]);
        }
        
        //获取数据
        $lists = $this->rows([],[
            Db::raw('CONCAT("",brand_id) AS brand_id'),
            'brand_name'
        ]);
        
        if( empty($lists) ) {
            $redis->set($key,self::REDIS_EMPTY_NULL_TAG,self::REDIS_CACHE_EXPIRE);
            return Normal::returnCode(Normal::SUC,"暂无数据");
        }
        
        $redis->set($key,json_encode($lists,JSON_UNESCAPED_UNICODE),self::REDIS_CACHE_EXPIRE);
        return Normal::returnCode(Normal::SUC,"获取数据成功",[
            'recommandBrandList' => $lists
        ]);
    }
}
